<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card" style="margin-top: 20px;">
      <div style=" width:600px; display:flex; ">
        <el-input
          placeholder="请输入内容"
          class="input-with-select"
          v-model="commodityForm.query"
          clearable
          style="margin-right:30px;"
          @clear="getCommodity"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getCommodity"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addpage">添加商品</el-button>
      </div>
      <el-table :data="commodityList" border style="width: 100%;margin-top: 20px;">
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="700"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="280"
        >
        <template v-slot="scope">
          {{scope.row.add_time | dateFormat}}
        </template>
        </el-table-column>
        <el-table-column label="操作" width="230">
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delCommodity(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="commodityForm.pagenum"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="commodityForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  // 组件注册
  components: {},
  // 父组件传参
  props: {},
  // 变量声明
  data() {
    return {
      commodityForm: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      totals: 0,
      commodityList: [],
    };
  },
  // Vue实例创建完成以后执行 数据可用
  created() {
    this.getCommodity();
  },
  // Vue模板挂载完毕以后执行  DOM节点加载完毕了（可以获取节点了）
  mounted() {},
  // keepalive 缓存进入执行
  activited() {},
  // vue组件更新完毕以后执行
  update() {},
  // 定义事件
  methods: {
    async getCommodity() {
      let res = await this.$axios.get("goods", { params: this.commodityForm });
      console.log(res);

      let { data, meta } = res.data;
      console.log(data);
      if (meta.status == 200) {
        this.commodityList = data.goods;
        this.totals = data.total;
      }
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.commodityForm.pagesize = val;
      this.getCommodity();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.commodityForm.pagenum = val;
      this.getCommodity();
    },
   delCommodity(id) {

       this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let res = await this.$axios.delete(`goods/${id}`); 
          let { data, meta } = res.data;
     if (meta.status == 200) {
           this.$message({
            type: 'success',
            message: '删除成功!'
          });

        this.getCommodity();
      }else{
        this.$message.error("删除失败")
      }

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
   

    },
    addpage(){
      this.$router.push("/add")
    }
  },
  // 过滤器
  filter: {},
  // 计算属性
  computed: {},
  // 监听属性
  watch: {},
};
</script>

<style scoped></style>



